/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.games.epkb.controller;

import it.polimi.games.epkb.controller.exceptions.IllegalOrphanException;
import it.polimi.games.epkb.controller.exceptions.NonexistentEntityException;
import it.polimi.games.epkb.controller.exceptions.PreexistingEntityException;
import it.polimi.games.epkb.entityClasses.Component;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import it.polimi.games.epkb.entityClasses.CoolingSystem;
import it.polimi.games.epkb.entityClasses.Disk;
import it.polimi.games.epkb.entityClasses.NetworkInterface;
import it.polimi.games.epkb.entityClasses.Rack;
import it.polimi.games.epkb.entityClasses.BusinessProcess;
import it.polimi.games.epkb.entityClasses.ServiceInstance;
import it.polimi.games.epkb.entityClasses.VirtualMachineInst;
import it.polimi.games.epkb.entityClasses.DiskArray;
import it.polimi.games.epkb.entityClasses.Activity;
import it.polimi.games.epkb.entityClasses.Cpu;
import it.polimi.games.epkb.entityClasses.Memory;
import it.polimi.games.epkb.entityClasses.Isle;
import it.polimi.games.epkb.entityClasses.Service;
import it.polimi.games.epkb.entityClasses.CpuCore;
import it.polimi.games.epkb.entityClasses.Server;
import it.polimi.games.epkb.entityClasses.Storage;
import it.polimi.games.epkb.entityClasses.Room;
import it.polimi.games.epkb.entityClasses.DataFile;
import java.util.ArrayList;
import java.util.Collection;
import it.polimi.games.epkb.entityClasses.SystemActionOnComponent;
import it.polimi.games.epkb.entityClasses.Annotation;
import it.polimi.games.epkb.entityClasses.Thresholds;
import it.polimi.games.epkb.entityClasses.ObservedComponent;
import it.polimi.games.epkb.entityClasses.GpiKpiInstance;
import it.polimi.games.epkb.entityClasses.StoredValues;
import it.polimi.games.epkb.entityClasses.VirtualMachineResources;
import javax.transaction.UserTransaction;

/**
 *
 * @author plebani
 */
public class ComponentJpaController implements Serializable {

    public ComponentJpaController(EntityManagerFactory emf) {
        this.utx = null;
        this.emf = emf;
    }

    public ComponentJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Component component) throws PreexistingEntityException, Exception {
        if (component.getDataFileCollection() == null) {
            component.setDataFileCollection(new ArrayList<DataFile>());
        }
        if (component.getSystemActionOnComponentCollection() == null) {
            component.setSystemActionOnComponentCollection(new ArrayList<SystemActionOnComponent>());
        }
        if (component.getAnnotationCollection() == null) {
            component.setAnnotationCollection(new ArrayList<Annotation>());
        }
        if (component.getThresholdsCollection() == null) {
            component.setThresholdsCollection(new ArrayList<Thresholds>());
        }
        if (component.getObservedComponentCollection() == null) {
            component.setObservedComponentCollection(new ArrayList<ObservedComponent>());
        }
        if (component.getGpiKpiInstanceCollection() == null) {
            component.setGpiKpiInstanceCollection(new ArrayList<GpiKpiInstance>());
        }
        if (component.getStoredValuesCollection() == null) {
            component.setStoredValuesCollection(new ArrayList<StoredValues>());
        }
        if (component.getVirtualMachineResourcesCollection() == null) {
            component.setVirtualMachineResourcesCollection(new ArrayList<VirtualMachineResources>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CoolingSystem coolingSystem = component.getCoolingSystem();
            if (coolingSystem != null) {
                coolingSystem = em.getReference(coolingSystem.getClass(), coolingSystem.getId());
                component.setCoolingSystem(coolingSystem);
            }
            Disk disk = component.getDisk();
            if (disk != null) {
                disk = em.getReference(disk.getClass(), disk.getId());
                component.setDisk(disk);
            }
            NetworkInterface networkInterface = component.getNetworkInterface();
            if (networkInterface != null) {
                networkInterface = em.getReference(networkInterface.getClass(), networkInterface.getId());
                component.setNetworkInterface(networkInterface);
            }
            Rack rack = component.getRack();
            if (rack != null) {
                rack = em.getReference(rack.getClass(), rack.getId());
                component.setRack(rack);
            }
            BusinessProcess businessProcess = component.getBusinessProcess();
            if (businessProcess != null) {
                businessProcess = em.getReference(businessProcess.getClass(), businessProcess.getId());
                component.setBusinessProcess(businessProcess);
            }
            ServiceInstance serviceInstance = component.getServiceInstance();
            if (serviceInstance != null) {
                serviceInstance = em.getReference(serviceInstance.getClass(), serviceInstance.getId());
                component.setServiceInstance(serviceInstance);
            }
            VirtualMachineInst virtualMachineInst = component.getVirtualMachineInst();
            if (virtualMachineInst != null) {
                virtualMachineInst = em.getReference(virtualMachineInst.getClass(), virtualMachineInst.getId());
                component.setVirtualMachineInst(virtualMachineInst);
            }
            DiskArray diskArray = component.getDiskArray();
            if (diskArray != null) {
                diskArray = em.getReference(diskArray.getClass(), diskArray.getId());
                component.setDiskArray(diskArray);
            }
            Activity activity = component.getActivity();
            if (activity != null) {
                activity = em.getReference(activity.getClass(), activity.getId());
                component.setActivity(activity);
            }
            Cpu cpu = component.getCpu();
            if (cpu != null) {
                cpu = em.getReference(cpu.getClass(), cpu.getId());
                component.setCpu(cpu);
            }
            Memory memory = component.getMemory();
            if (memory != null) {
                memory = em.getReference(memory.getClass(), memory.getId());
                component.setMemory(memory);
            }
            Isle isle = component.getIsle();
            if (isle != null) {
                isle = em.getReference(isle.getClass(), isle.getId());
                component.setIsle(isle);
            }
            Service service = component.getService();
            if (service != null) {
                service = em.getReference(service.getClass(), service.getId());
                component.setService(service);
            }
            CpuCore cpuCore = component.getCpuCore();
            if (cpuCore != null) {
                cpuCore = em.getReference(cpuCore.getClass(), cpuCore.getId());
                component.setCpuCore(cpuCore);
            }
            Server server = component.getServer();
            if (server != null) {
                server = em.getReference(server.getClass(), server.getId());
                component.setServer(server);
            }
            Storage storage = component.getStorage();
            if (storage != null) {
                storage = em.getReference(storage.getClass(), storage.getId());
                component.setStorage(storage);
            }
            Room room = component.getRoom();
            if (room != null) {
                room = em.getReference(room.getClass(), room.getId());
                component.setRoom(room);
            }
            Collection<DataFile> attachedDataFileCollection = new ArrayList<DataFile>();
            for (DataFile dataFileCollectionDataFileToAttach : component.getDataFileCollection()) {
                dataFileCollectionDataFileToAttach = em.getReference(dataFileCollectionDataFileToAttach.getClass(), dataFileCollectionDataFileToAttach.getId());
                attachedDataFileCollection.add(dataFileCollectionDataFileToAttach);
            }
            component.setDataFileCollection(attachedDataFileCollection);
            Collection<SystemActionOnComponent> attachedSystemActionOnComponentCollection = new ArrayList<SystemActionOnComponent>();
            for (SystemActionOnComponent systemActionOnComponentCollectionSystemActionOnComponentToAttach : component.getSystemActionOnComponentCollection()) {
                systemActionOnComponentCollectionSystemActionOnComponentToAttach = em.getReference(systemActionOnComponentCollectionSystemActionOnComponentToAttach.getClass(), systemActionOnComponentCollectionSystemActionOnComponentToAttach.getSystemActionOnComponentPK());
                attachedSystemActionOnComponentCollection.add(systemActionOnComponentCollectionSystemActionOnComponentToAttach);
            }
            component.setSystemActionOnComponentCollection(attachedSystemActionOnComponentCollection);
            Collection<Annotation> attachedAnnotationCollection = new ArrayList<Annotation>();
            for (Annotation annotationCollectionAnnotationToAttach : component.getAnnotationCollection()) {
                annotationCollectionAnnotationToAttach = em.getReference(annotationCollectionAnnotationToAttach.getClass(), annotationCollectionAnnotationToAttach.getId());
                attachedAnnotationCollection.add(annotationCollectionAnnotationToAttach);
            }
            component.setAnnotationCollection(attachedAnnotationCollection);
            Collection<Thresholds> attachedThresholdsCollection = new ArrayList<Thresholds>();
            for (Thresholds thresholdsCollectionThresholdsToAttach : component.getThresholdsCollection()) {
                thresholdsCollectionThresholdsToAttach = em.getReference(thresholdsCollectionThresholdsToAttach.getClass(), thresholdsCollectionThresholdsToAttach.getThresholdsPK());
                attachedThresholdsCollection.add(thresholdsCollectionThresholdsToAttach);
            }
            component.setThresholdsCollection(attachedThresholdsCollection);
            Collection<ObservedComponent> attachedObservedComponentCollection = new ArrayList<ObservedComponent>();
            for (ObservedComponent observedComponentCollectionObservedComponentToAttach : component.getObservedComponentCollection()) {
                observedComponentCollectionObservedComponentToAttach = em.getReference(observedComponentCollectionObservedComponentToAttach.getClass(), observedComponentCollectionObservedComponentToAttach.getObservedComponentPK());
                attachedObservedComponentCollection.add(observedComponentCollectionObservedComponentToAttach);
            }
            component.setObservedComponentCollection(attachedObservedComponentCollection);
            Collection<GpiKpiInstance> attachedGpiKpiInstanceCollection = new ArrayList<GpiKpiInstance>();
            for (GpiKpiInstance gpiKpiInstanceCollectionGpiKpiInstanceToAttach : component.getGpiKpiInstanceCollection()) {
                gpiKpiInstanceCollectionGpiKpiInstanceToAttach = em.getReference(gpiKpiInstanceCollectionGpiKpiInstanceToAttach.getClass(), gpiKpiInstanceCollectionGpiKpiInstanceToAttach.getGpiKpiInstancePK());
                attachedGpiKpiInstanceCollection.add(gpiKpiInstanceCollectionGpiKpiInstanceToAttach);
            }
            component.setGpiKpiInstanceCollection(attachedGpiKpiInstanceCollection);
            Collection<StoredValues> attachedStoredValuesCollection = new ArrayList<StoredValues>();
            for (StoredValues storedValuesCollectionStoredValuesToAttach : component.getStoredValuesCollection()) {
                storedValuesCollectionStoredValuesToAttach = em.getReference(storedValuesCollectionStoredValuesToAttach.getClass(), storedValuesCollectionStoredValuesToAttach.getId());
                attachedStoredValuesCollection.add(storedValuesCollectionStoredValuesToAttach);
            }
            component.setStoredValuesCollection(attachedStoredValuesCollection);
            Collection<VirtualMachineResources> attachedVirtualMachineResourcesCollection = new ArrayList<VirtualMachineResources>();
            for (VirtualMachineResources virtualMachineResourcesCollectionVirtualMachineResourcesToAttach : component.getVirtualMachineResourcesCollection()) {
                virtualMachineResourcesCollectionVirtualMachineResourcesToAttach = em.getReference(virtualMachineResourcesCollectionVirtualMachineResourcesToAttach.getClass(), virtualMachineResourcesCollectionVirtualMachineResourcesToAttach.getVirtualMachineResourcesPK());
                attachedVirtualMachineResourcesCollection.add(virtualMachineResourcesCollectionVirtualMachineResourcesToAttach);
            }
            component.setVirtualMachineResourcesCollection(attachedVirtualMachineResourcesCollection);
            em.persist(component);
            if (coolingSystem != null) {
                Component oldComponentOfCoolingSystem = coolingSystem.getComponent();
                if (oldComponentOfCoolingSystem != null) {
                    oldComponentOfCoolingSystem.setCoolingSystem(null);
                    oldComponentOfCoolingSystem = em.merge(oldComponentOfCoolingSystem);
                }
                coolingSystem.setComponent(component);
                coolingSystem = em.merge(coolingSystem);
            }
            if (disk != null) {
                Component oldComponentOfDisk = disk.getComponent();
                if (oldComponentOfDisk != null) {
                    oldComponentOfDisk.setDisk(null);
                    oldComponentOfDisk = em.merge(oldComponentOfDisk);
                }
                disk.setComponent(component);
                disk = em.merge(disk);
            }
            if (networkInterface != null) {
                Component oldComponentOfNetworkInterface = networkInterface.getComponent();
                if (oldComponentOfNetworkInterface != null) {
                    oldComponentOfNetworkInterface.setNetworkInterface(null);
                    oldComponentOfNetworkInterface = em.merge(oldComponentOfNetworkInterface);
                }
                networkInterface.setComponent(component);
                networkInterface = em.merge(networkInterface);
            }
            if (rack != null) {
                Component oldComponentOfRack = rack.getComponent();
                if (oldComponentOfRack != null) {
                    oldComponentOfRack.setRack(null);
                    oldComponentOfRack = em.merge(oldComponentOfRack);
                }
                rack.setComponent(component);
                rack = em.merge(rack);
            }
            if (businessProcess != null) {
                Component oldComponentOfBusinessProcess = businessProcess.getComponent();
                if (oldComponentOfBusinessProcess != null) {
                    oldComponentOfBusinessProcess.setBusinessProcess(null);
                    oldComponentOfBusinessProcess = em.merge(oldComponentOfBusinessProcess);
                }
                businessProcess.setComponent(component);
                businessProcess = em.merge(businessProcess);
            }
            if (serviceInstance != null) {
                Component oldComponentOfServiceInstance = serviceInstance.getComponent();
                if (oldComponentOfServiceInstance != null) {
                    oldComponentOfServiceInstance.setServiceInstance(null);
                    oldComponentOfServiceInstance = em.merge(oldComponentOfServiceInstance);
                }
                serviceInstance.setComponent(component);
                serviceInstance = em.merge(serviceInstance);
            }
            if (virtualMachineInst != null) {
                Component oldComponentOfVirtualMachineInst = virtualMachineInst.getComponent();
                if (oldComponentOfVirtualMachineInst != null) {
                    oldComponentOfVirtualMachineInst.setVirtualMachineInst(null);
                    oldComponentOfVirtualMachineInst = em.merge(oldComponentOfVirtualMachineInst);
                }
                virtualMachineInst.setComponent(component);
                virtualMachineInst = em.merge(virtualMachineInst);
            }
            if (diskArray != null) {
                Component oldComponentOfDiskArray = diskArray.getComponent();
                if (oldComponentOfDiskArray != null) {
                    oldComponentOfDiskArray.setDiskArray(null);
                    oldComponentOfDiskArray = em.merge(oldComponentOfDiskArray);
                }
                diskArray.setComponent(component);
                diskArray = em.merge(diskArray);
            }
            if (activity != null) {
                Component oldComponentOfActivity = activity.getComponent();
                if (oldComponentOfActivity != null) {
                    oldComponentOfActivity.setActivity(null);
                    oldComponentOfActivity = em.merge(oldComponentOfActivity);
                }
                activity.setComponent(component);
                activity = em.merge(activity);
            }
            if (cpu != null) {
                Component oldComponentOfCpu = cpu.getComponent();
                if (oldComponentOfCpu != null) {
                    oldComponentOfCpu.setCpu(null);
                    oldComponentOfCpu = em.merge(oldComponentOfCpu);
                }
                cpu.setComponent(component);
                cpu = em.merge(cpu);
            }
            if (memory != null) {
                Component oldComponentOfMemory = memory.getComponent();
                if (oldComponentOfMemory != null) {
                    oldComponentOfMemory.setMemory(null);
                    oldComponentOfMemory = em.merge(oldComponentOfMemory);
                }
                memory.setComponent(component);
                memory = em.merge(memory);
            }
            if (isle != null) {
                Component oldComponentOfIsle = isle.getComponent();
                if (oldComponentOfIsle != null) {
                    oldComponentOfIsle.setIsle(null);
                    oldComponentOfIsle = em.merge(oldComponentOfIsle);
                }
                isle.setComponent(component);
                isle = em.merge(isle);
            }
            if (service != null) {
                Component oldComponentOfService = service.getComponent();
                if (oldComponentOfService != null) {
                    oldComponentOfService.setService(null);
                    oldComponentOfService = em.merge(oldComponentOfService);
                }
                service.setComponent(component);
                service = em.merge(service);
            }
            if (cpuCore != null) {
                Component oldComponentOfCpuCore = cpuCore.getComponent();
                if (oldComponentOfCpuCore != null) {
                    oldComponentOfCpuCore.setCpuCore(null);
                    oldComponentOfCpuCore = em.merge(oldComponentOfCpuCore);
                }
                cpuCore.setComponent(component);
                cpuCore = em.merge(cpuCore);
            }
            if (server != null) {
                Component oldComponentOfServer = server.getComponent();
                if (oldComponentOfServer != null) {
                    oldComponentOfServer.setServer(null);
                    oldComponentOfServer = em.merge(oldComponentOfServer);
                }
                server.setComponent(component);
                server = em.merge(server);
            }
            if (storage != null) {
                Component oldComponentOfStorage = storage.getComponent();
                if (oldComponentOfStorage != null) {
                    oldComponentOfStorage.setStorage(null);
                    oldComponentOfStorage = em.merge(oldComponentOfStorage);
                }
                storage.setComponent(component);
                storage = em.merge(storage);
            }
            if (room != null) {
                Component oldComponentOfRoom = room.getComponent();
                if (oldComponentOfRoom != null) {
                    oldComponentOfRoom.setRoom(null);
                    oldComponentOfRoom = em.merge(oldComponentOfRoom);
                }
                room.setComponent(component);
                room = em.merge(room);
            }
            for (DataFile dataFileCollectionDataFile : component.getDataFileCollection()) {
                dataFileCollectionDataFile.getComponentCollection().add(component);
                dataFileCollectionDataFile = em.merge(dataFileCollectionDataFile);
            }
            for (SystemActionOnComponent systemActionOnComponentCollectionSystemActionOnComponent : component.getSystemActionOnComponentCollection()) {
                Component oldComponentOfSystemActionOnComponentCollectionSystemActionOnComponent = systemActionOnComponentCollectionSystemActionOnComponent.getComponent();
                systemActionOnComponentCollectionSystemActionOnComponent.setComponent(component);
                systemActionOnComponentCollectionSystemActionOnComponent = em.merge(systemActionOnComponentCollectionSystemActionOnComponent);
                if (oldComponentOfSystemActionOnComponentCollectionSystemActionOnComponent != null) {
                    oldComponentOfSystemActionOnComponentCollectionSystemActionOnComponent.getSystemActionOnComponentCollection().remove(systemActionOnComponentCollectionSystemActionOnComponent);
                    oldComponentOfSystemActionOnComponentCollectionSystemActionOnComponent = em.merge(oldComponentOfSystemActionOnComponentCollectionSystemActionOnComponent);
                }
            }
            for (Annotation annotationCollectionAnnotation : component.getAnnotationCollection()) {
                Component oldIdComponentOfAnnotationCollectionAnnotation = annotationCollectionAnnotation.getIdComponent();
                annotationCollectionAnnotation.setIdComponent(component);
                annotationCollectionAnnotation = em.merge(annotationCollectionAnnotation);
                if (oldIdComponentOfAnnotationCollectionAnnotation != null) {
                    oldIdComponentOfAnnotationCollectionAnnotation.getAnnotationCollection().remove(annotationCollectionAnnotation);
                    oldIdComponentOfAnnotationCollectionAnnotation = em.merge(oldIdComponentOfAnnotationCollectionAnnotation);
                }
            }
            for (Thresholds thresholdsCollectionThresholds : component.getThresholdsCollection()) {
                Component oldComponentOfThresholdsCollectionThresholds = thresholdsCollectionThresholds.getComponent();
                thresholdsCollectionThresholds.setComponent(component);
                thresholdsCollectionThresholds = em.merge(thresholdsCollectionThresholds);
                if (oldComponentOfThresholdsCollectionThresholds != null) {
                    oldComponentOfThresholdsCollectionThresholds.getThresholdsCollection().remove(thresholdsCollectionThresholds);
                    oldComponentOfThresholdsCollectionThresholds = em.merge(oldComponentOfThresholdsCollectionThresholds);
                }
            }
            for (ObservedComponent observedComponentCollectionObservedComponent : component.getObservedComponentCollection()) {
                Component oldComponentOfObservedComponentCollectionObservedComponent = observedComponentCollectionObservedComponent.getComponent();
                observedComponentCollectionObservedComponent.setComponent(component);
                observedComponentCollectionObservedComponent = em.merge(observedComponentCollectionObservedComponent);
                if (oldComponentOfObservedComponentCollectionObservedComponent != null) {
                    oldComponentOfObservedComponentCollectionObservedComponent.getObservedComponentCollection().remove(observedComponentCollectionObservedComponent);
                    oldComponentOfObservedComponentCollectionObservedComponent = em.merge(oldComponentOfObservedComponentCollectionObservedComponent);
                }
            }
            for (GpiKpiInstance gpiKpiInstanceCollectionGpiKpiInstance : component.getGpiKpiInstanceCollection()) {
                Component oldIdComponentOfGpiKpiInstanceCollectionGpiKpiInstance = gpiKpiInstanceCollectionGpiKpiInstance.getIdComponent();
                gpiKpiInstanceCollectionGpiKpiInstance.setIdComponent(component);
                gpiKpiInstanceCollectionGpiKpiInstance = em.merge(gpiKpiInstanceCollectionGpiKpiInstance);
                if (oldIdComponentOfGpiKpiInstanceCollectionGpiKpiInstance != null) {
                    oldIdComponentOfGpiKpiInstanceCollectionGpiKpiInstance.getGpiKpiInstanceCollection().remove(gpiKpiInstanceCollectionGpiKpiInstance);
                    oldIdComponentOfGpiKpiInstanceCollectionGpiKpiInstance = em.merge(oldIdComponentOfGpiKpiInstanceCollectionGpiKpiInstance);
                }
            }
            for (StoredValues storedValuesCollectionStoredValues : component.getStoredValuesCollection()) {
                Component oldIdComponentOfStoredValuesCollectionStoredValues = storedValuesCollectionStoredValues.getIdComponent();
                storedValuesCollectionStoredValues.setIdComponent(component);
                storedValuesCollectionStoredValues = em.merge(storedValuesCollectionStoredValues);
                if (oldIdComponentOfStoredValuesCollectionStoredValues != null) {
                    oldIdComponentOfStoredValuesCollectionStoredValues.getStoredValuesCollection().remove(storedValuesCollectionStoredValues);
                    oldIdComponentOfStoredValuesCollectionStoredValues = em.merge(oldIdComponentOfStoredValuesCollectionStoredValues);
                }
            }
            for (VirtualMachineResources virtualMachineResourcesCollectionVirtualMachineResources : component.getVirtualMachineResourcesCollection()) {
                Component oldComponentOfVirtualMachineResourcesCollectionVirtualMachineResources = virtualMachineResourcesCollectionVirtualMachineResources.getComponent();
                virtualMachineResourcesCollectionVirtualMachineResources.setComponent(component);
                virtualMachineResourcesCollectionVirtualMachineResources = em.merge(virtualMachineResourcesCollectionVirtualMachineResources);
                if (oldComponentOfVirtualMachineResourcesCollectionVirtualMachineResources != null) {
                    oldComponentOfVirtualMachineResourcesCollectionVirtualMachineResources.getVirtualMachineResourcesCollection().remove(virtualMachineResourcesCollectionVirtualMachineResources);
                    oldComponentOfVirtualMachineResourcesCollectionVirtualMachineResources = em.merge(oldComponentOfVirtualMachineResourcesCollectionVirtualMachineResources);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findComponent(component.getId()) != null) {
                throw new PreexistingEntityException("Component " + component + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Component component) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Component persistentComponent = em.find(Component.class, component.getId());
            CoolingSystem coolingSystemOld = persistentComponent.getCoolingSystem();
            CoolingSystem coolingSystemNew = component.getCoolingSystem();
            Disk diskOld = persistentComponent.getDisk();
            Disk diskNew = component.getDisk();
            NetworkInterface networkInterfaceOld = persistentComponent.getNetworkInterface();
            NetworkInterface networkInterfaceNew = component.getNetworkInterface();
            Rack rackOld = persistentComponent.getRack();
            Rack rackNew = component.getRack();
            BusinessProcess businessProcessOld = persistentComponent.getBusinessProcess();
            BusinessProcess businessProcessNew = component.getBusinessProcess();
            ServiceInstance serviceInstanceOld = persistentComponent.getServiceInstance();
            ServiceInstance serviceInstanceNew = component.getServiceInstance();
            VirtualMachineInst virtualMachineInstOld = persistentComponent.getVirtualMachineInst();
            VirtualMachineInst virtualMachineInstNew = component.getVirtualMachineInst();
            DiskArray diskArrayOld = persistentComponent.getDiskArray();
            DiskArray diskArrayNew = component.getDiskArray();
            Activity activityOld = persistentComponent.getActivity();
            Activity activityNew = component.getActivity();
            Cpu cpuOld = persistentComponent.getCpu();
            Cpu cpuNew = component.getCpu();
            Memory memoryOld = persistentComponent.getMemory();
            Memory memoryNew = component.getMemory();
            Isle isleOld = persistentComponent.getIsle();
            Isle isleNew = component.getIsle();
            Service serviceOld = persistentComponent.getService();
            Service serviceNew = component.getService();
            CpuCore cpuCoreOld = persistentComponent.getCpuCore();
            CpuCore cpuCoreNew = component.getCpuCore();
            Server serverOld = persistentComponent.getServer();
            Server serverNew = component.getServer();
            Storage storageOld = persistentComponent.getStorage();
            Storage storageNew = component.getStorage();
            Room roomOld = persistentComponent.getRoom();
            Room roomNew = component.getRoom();
            Collection<DataFile> dataFileCollectionOld = persistentComponent.getDataFileCollection();
            Collection<DataFile> dataFileCollectionNew = component.getDataFileCollection();
            Collection<SystemActionOnComponent> systemActionOnComponentCollectionOld = persistentComponent.getSystemActionOnComponentCollection();
            Collection<SystemActionOnComponent> systemActionOnComponentCollectionNew = component.getSystemActionOnComponentCollection();
            Collection<Annotation> annotationCollectionOld = persistentComponent.getAnnotationCollection();
            Collection<Annotation> annotationCollectionNew = component.getAnnotationCollection();
            Collection<Thresholds> thresholdsCollectionOld = persistentComponent.getThresholdsCollection();
            Collection<Thresholds> thresholdsCollectionNew = component.getThresholdsCollection();
            Collection<ObservedComponent> observedComponentCollectionOld = persistentComponent.getObservedComponentCollection();
            Collection<ObservedComponent> observedComponentCollectionNew = component.getObservedComponentCollection();
            Collection<GpiKpiInstance> gpiKpiInstanceCollectionOld = persistentComponent.getGpiKpiInstanceCollection();
            Collection<GpiKpiInstance> gpiKpiInstanceCollectionNew = component.getGpiKpiInstanceCollection();
            Collection<StoredValues> storedValuesCollectionOld = persistentComponent.getStoredValuesCollection();
            Collection<StoredValues> storedValuesCollectionNew = component.getStoredValuesCollection();
            Collection<VirtualMachineResources> virtualMachineResourcesCollectionOld = persistentComponent.getVirtualMachineResourcesCollection();
            Collection<VirtualMachineResources> virtualMachineResourcesCollectionNew = component.getVirtualMachineResourcesCollection();
            List<String> illegalOrphanMessages = null;
            if (coolingSystemOld != null && !coolingSystemOld.equals(coolingSystemNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain CoolingSystem " + coolingSystemOld + " since its component field is not nullable.");
            }
            if (diskOld != null && !diskOld.equals(diskNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain Disk " + diskOld + " since its component field is not nullable.");
            }
            if (networkInterfaceOld != null && !networkInterfaceOld.equals(networkInterfaceNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain NetworkInterface " + networkInterfaceOld + " since its component field is not nullable.");
            }
            if (rackOld != null && !rackOld.equals(rackNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain Rack " + rackOld + " since its component field is not nullable.");
            }
            if (businessProcessOld != null && !businessProcessOld.equals(businessProcessNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain BusinessProcess " + businessProcessOld + " since its component field is not nullable.");
            }
            if (serviceInstanceOld != null && !serviceInstanceOld.equals(serviceInstanceNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain ServiceInstance " + serviceInstanceOld + " since its component field is not nullable.");
            }
            if (virtualMachineInstOld != null && !virtualMachineInstOld.equals(virtualMachineInstNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain VirtualMachineInst " + virtualMachineInstOld + " since its component field is not nullable.");
            }
            if (diskArrayOld != null && !diskArrayOld.equals(diskArrayNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain DiskArray " + diskArrayOld + " since its component field is not nullable.");
            }
            if (activityOld != null && !activityOld.equals(activityNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain Activity " + activityOld + " since its component field is not nullable.");
            }
            if (cpuOld != null && !cpuOld.equals(cpuNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain Cpu " + cpuOld + " since its component field is not nullable.");
            }
            if (memoryOld != null && !memoryOld.equals(memoryNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain Memory " + memoryOld + " since its component field is not nullable.");
            }
            if (isleOld != null && !isleOld.equals(isleNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain Isle " + isleOld + " since its component field is not nullable.");
            }
            if (serviceOld != null && !serviceOld.equals(serviceNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain Service " + serviceOld + " since its component field is not nullable.");
            }
            if (cpuCoreOld != null && !cpuCoreOld.equals(cpuCoreNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain CpuCore " + cpuCoreOld + " since its component field is not nullable.");
            }
            if (serverOld != null && !serverOld.equals(serverNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain Server " + serverOld + " since its component field is not nullable.");
            }
            if (storageOld != null && !storageOld.equals(storageNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain Storage " + storageOld + " since its component field is not nullable.");
            }
            if (roomOld != null && !roomOld.equals(roomNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain Room " + roomOld + " since its component field is not nullable.");
            }
            for (SystemActionOnComponent systemActionOnComponentCollectionOldSystemActionOnComponent : systemActionOnComponentCollectionOld) {
                if (!systemActionOnComponentCollectionNew.contains(systemActionOnComponentCollectionOldSystemActionOnComponent)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain SystemActionOnComponent " + systemActionOnComponentCollectionOldSystemActionOnComponent + " since its component field is not nullable.");
                }
            }
            for (Thresholds thresholdsCollectionOldThresholds : thresholdsCollectionOld) {
                if (!thresholdsCollectionNew.contains(thresholdsCollectionOldThresholds)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Thresholds " + thresholdsCollectionOldThresholds + " since its component field is not nullable.");
                }
            }
            for (ObservedComponent observedComponentCollectionOldObservedComponent : observedComponentCollectionOld) {
                if (!observedComponentCollectionNew.contains(observedComponentCollectionOldObservedComponent)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain ObservedComponent " + observedComponentCollectionOldObservedComponent + " since its component field is not nullable.");
                }
            }
            for (StoredValues storedValuesCollectionOldStoredValues : storedValuesCollectionOld) {
                if (!storedValuesCollectionNew.contains(storedValuesCollectionOldStoredValues)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain StoredValues " + storedValuesCollectionOldStoredValues + " since its idComponent field is not nullable.");
                }
            }
            for (VirtualMachineResources virtualMachineResourcesCollectionOldVirtualMachineResources : virtualMachineResourcesCollectionOld) {
                if (!virtualMachineResourcesCollectionNew.contains(virtualMachineResourcesCollectionOldVirtualMachineResources)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain VirtualMachineResources " + virtualMachineResourcesCollectionOldVirtualMachineResources + " since its component field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (coolingSystemNew != null) {
                coolingSystemNew = em.getReference(coolingSystemNew.getClass(), coolingSystemNew.getId());
                component.setCoolingSystem(coolingSystemNew);
            }
            if (diskNew != null) {
                diskNew = em.getReference(diskNew.getClass(), diskNew.getId());
                component.setDisk(diskNew);
            }
            if (networkInterfaceNew != null) {
                networkInterfaceNew = em.getReference(networkInterfaceNew.getClass(), networkInterfaceNew.getId());
                component.setNetworkInterface(networkInterfaceNew);
            }
            if (rackNew != null) {
                rackNew = em.getReference(rackNew.getClass(), rackNew.getId());
                component.setRack(rackNew);
            }
            if (businessProcessNew != null) {
                businessProcessNew = em.getReference(businessProcessNew.getClass(), businessProcessNew.getId());
                component.setBusinessProcess(businessProcessNew);
            }
            if (serviceInstanceNew != null) {
                serviceInstanceNew = em.getReference(serviceInstanceNew.getClass(), serviceInstanceNew.getId());
                component.setServiceInstance(serviceInstanceNew);
            }
            if (virtualMachineInstNew != null) {
                virtualMachineInstNew = em.getReference(virtualMachineInstNew.getClass(), virtualMachineInstNew.getId());
                component.setVirtualMachineInst(virtualMachineInstNew);
            }
            if (diskArrayNew != null) {
                diskArrayNew = em.getReference(diskArrayNew.getClass(), diskArrayNew.getId());
                component.setDiskArray(diskArrayNew);
            }
            if (activityNew != null) {
                activityNew = em.getReference(activityNew.getClass(), activityNew.getId());
                component.setActivity(activityNew);
            }
            if (cpuNew != null) {
                cpuNew = em.getReference(cpuNew.getClass(), cpuNew.getId());
                component.setCpu(cpuNew);
            }
            if (memoryNew != null) {
                memoryNew = em.getReference(memoryNew.getClass(), memoryNew.getId());
                component.setMemory(memoryNew);
            }
            if (isleNew != null) {
                isleNew = em.getReference(isleNew.getClass(), isleNew.getId());
                component.setIsle(isleNew);
            }
            if (serviceNew != null) {
                serviceNew = em.getReference(serviceNew.getClass(), serviceNew.getId());
                component.setService(serviceNew);
            }
            if (cpuCoreNew != null) {
                cpuCoreNew = em.getReference(cpuCoreNew.getClass(), cpuCoreNew.getId());
                component.setCpuCore(cpuCoreNew);
            }
            if (serverNew != null) {
                serverNew = em.getReference(serverNew.getClass(), serverNew.getId());
                component.setServer(serverNew);
            }
            if (storageNew != null) {
                storageNew = em.getReference(storageNew.getClass(), storageNew.getId());
                component.setStorage(storageNew);
            }
            if (roomNew != null) {
                roomNew = em.getReference(roomNew.getClass(), roomNew.getId());
                component.setRoom(roomNew);
            }
            Collection<DataFile> attachedDataFileCollectionNew = new ArrayList<DataFile>();
            for (DataFile dataFileCollectionNewDataFileToAttach : dataFileCollectionNew) {
                dataFileCollectionNewDataFileToAttach = em.getReference(dataFileCollectionNewDataFileToAttach.getClass(), dataFileCollectionNewDataFileToAttach.getId());
                attachedDataFileCollectionNew.add(dataFileCollectionNewDataFileToAttach);
            }
            dataFileCollectionNew = attachedDataFileCollectionNew;
            component.setDataFileCollection(dataFileCollectionNew);
            Collection<SystemActionOnComponent> attachedSystemActionOnComponentCollectionNew = new ArrayList<SystemActionOnComponent>();
            for (SystemActionOnComponent systemActionOnComponentCollectionNewSystemActionOnComponentToAttach : systemActionOnComponentCollectionNew) {
                systemActionOnComponentCollectionNewSystemActionOnComponentToAttach = em.getReference(systemActionOnComponentCollectionNewSystemActionOnComponentToAttach.getClass(), systemActionOnComponentCollectionNewSystemActionOnComponentToAttach.getSystemActionOnComponentPK());
                attachedSystemActionOnComponentCollectionNew.add(systemActionOnComponentCollectionNewSystemActionOnComponentToAttach);
            }
            systemActionOnComponentCollectionNew = attachedSystemActionOnComponentCollectionNew;
            component.setSystemActionOnComponentCollection(systemActionOnComponentCollectionNew);
            Collection<Annotation> attachedAnnotationCollectionNew = new ArrayList<Annotation>();
            for (Annotation annotationCollectionNewAnnotationToAttach : annotationCollectionNew) {
                annotationCollectionNewAnnotationToAttach = em.getReference(annotationCollectionNewAnnotationToAttach.getClass(), annotationCollectionNewAnnotationToAttach.getId());
                attachedAnnotationCollectionNew.add(annotationCollectionNewAnnotationToAttach);
            }
            annotationCollectionNew = attachedAnnotationCollectionNew;
            component.setAnnotationCollection(annotationCollectionNew);
            Collection<Thresholds> attachedThresholdsCollectionNew = new ArrayList<Thresholds>();
            for (Thresholds thresholdsCollectionNewThresholdsToAttach : thresholdsCollectionNew) {
                thresholdsCollectionNewThresholdsToAttach = em.getReference(thresholdsCollectionNewThresholdsToAttach.getClass(), thresholdsCollectionNewThresholdsToAttach.getThresholdsPK());
                attachedThresholdsCollectionNew.add(thresholdsCollectionNewThresholdsToAttach);
            }
            thresholdsCollectionNew = attachedThresholdsCollectionNew;
            component.setThresholdsCollection(thresholdsCollectionNew);
            Collection<ObservedComponent> attachedObservedComponentCollectionNew = new ArrayList<ObservedComponent>();
            for (ObservedComponent observedComponentCollectionNewObservedComponentToAttach : observedComponentCollectionNew) {
                observedComponentCollectionNewObservedComponentToAttach = em.getReference(observedComponentCollectionNewObservedComponentToAttach.getClass(), observedComponentCollectionNewObservedComponentToAttach.getObservedComponentPK());
                attachedObservedComponentCollectionNew.add(observedComponentCollectionNewObservedComponentToAttach);
            }
            observedComponentCollectionNew = attachedObservedComponentCollectionNew;
            component.setObservedComponentCollection(observedComponentCollectionNew);
            Collection<GpiKpiInstance> attachedGpiKpiInstanceCollectionNew = new ArrayList<GpiKpiInstance>();
            for (GpiKpiInstance gpiKpiInstanceCollectionNewGpiKpiInstanceToAttach : gpiKpiInstanceCollectionNew) {
                gpiKpiInstanceCollectionNewGpiKpiInstanceToAttach = em.getReference(gpiKpiInstanceCollectionNewGpiKpiInstanceToAttach.getClass(), gpiKpiInstanceCollectionNewGpiKpiInstanceToAttach.getGpiKpiInstancePK());
                attachedGpiKpiInstanceCollectionNew.add(gpiKpiInstanceCollectionNewGpiKpiInstanceToAttach);
            }
            gpiKpiInstanceCollectionNew = attachedGpiKpiInstanceCollectionNew;
            component.setGpiKpiInstanceCollection(gpiKpiInstanceCollectionNew);
            Collection<StoredValues> attachedStoredValuesCollectionNew = new ArrayList<StoredValues>();
            for (StoredValues storedValuesCollectionNewStoredValuesToAttach : storedValuesCollectionNew) {
                storedValuesCollectionNewStoredValuesToAttach = em.getReference(storedValuesCollectionNewStoredValuesToAttach.getClass(), storedValuesCollectionNewStoredValuesToAttach.getId());
                attachedStoredValuesCollectionNew.add(storedValuesCollectionNewStoredValuesToAttach);
            }
            storedValuesCollectionNew = attachedStoredValuesCollectionNew;
            component.setStoredValuesCollection(storedValuesCollectionNew);
            Collection<VirtualMachineResources> attachedVirtualMachineResourcesCollectionNew = new ArrayList<VirtualMachineResources>();
            for (VirtualMachineResources virtualMachineResourcesCollectionNewVirtualMachineResourcesToAttach : virtualMachineResourcesCollectionNew) {
                virtualMachineResourcesCollectionNewVirtualMachineResourcesToAttach = em.getReference(virtualMachineResourcesCollectionNewVirtualMachineResourcesToAttach.getClass(), virtualMachineResourcesCollectionNewVirtualMachineResourcesToAttach.getVirtualMachineResourcesPK());
                attachedVirtualMachineResourcesCollectionNew.add(virtualMachineResourcesCollectionNewVirtualMachineResourcesToAttach);
            }
            virtualMachineResourcesCollectionNew = attachedVirtualMachineResourcesCollectionNew;
            component.setVirtualMachineResourcesCollection(virtualMachineResourcesCollectionNew);
            component = em.merge(component);
            if (coolingSystemNew != null && !coolingSystemNew.equals(coolingSystemOld)) {
                Component oldComponentOfCoolingSystem = coolingSystemNew.getComponent();
                if (oldComponentOfCoolingSystem != null) {
                    oldComponentOfCoolingSystem.setCoolingSystem(null);
                    oldComponentOfCoolingSystem = em.merge(oldComponentOfCoolingSystem);
                }
                coolingSystemNew.setComponent(component);
                coolingSystemNew = em.merge(coolingSystemNew);
            }
            if (diskNew != null && !diskNew.equals(diskOld)) {
                Component oldComponentOfDisk = diskNew.getComponent();
                if (oldComponentOfDisk != null) {
                    oldComponentOfDisk.setDisk(null);
                    oldComponentOfDisk = em.merge(oldComponentOfDisk);
                }
                diskNew.setComponent(component);
                diskNew = em.merge(diskNew);
            }
            if (networkInterfaceNew != null && !networkInterfaceNew.equals(networkInterfaceOld)) {
                Component oldComponentOfNetworkInterface = networkInterfaceNew.getComponent();
                if (oldComponentOfNetworkInterface != null) {
                    oldComponentOfNetworkInterface.setNetworkInterface(null);
                    oldComponentOfNetworkInterface = em.merge(oldComponentOfNetworkInterface);
                }
                networkInterfaceNew.setComponent(component);
                networkInterfaceNew = em.merge(networkInterfaceNew);
            }
            if (rackNew != null && !rackNew.equals(rackOld)) {
                Component oldComponentOfRack = rackNew.getComponent();
                if (oldComponentOfRack != null) {
                    oldComponentOfRack.setRack(null);
                    oldComponentOfRack = em.merge(oldComponentOfRack);
                }
                rackNew.setComponent(component);
                rackNew = em.merge(rackNew);
            }
            if (businessProcessNew != null && !businessProcessNew.equals(businessProcessOld)) {
                Component oldComponentOfBusinessProcess = businessProcessNew.getComponent();
                if (oldComponentOfBusinessProcess != null) {
                    oldComponentOfBusinessProcess.setBusinessProcess(null);
                    oldComponentOfBusinessProcess = em.merge(oldComponentOfBusinessProcess);
                }
                businessProcessNew.setComponent(component);
                businessProcessNew = em.merge(businessProcessNew);
            }
            if (serviceInstanceNew != null && !serviceInstanceNew.equals(serviceInstanceOld)) {
                Component oldComponentOfServiceInstance = serviceInstanceNew.getComponent();
                if (oldComponentOfServiceInstance != null) {
                    oldComponentOfServiceInstance.setServiceInstance(null);
                    oldComponentOfServiceInstance = em.merge(oldComponentOfServiceInstance);
                }
                serviceInstanceNew.setComponent(component);
                serviceInstanceNew = em.merge(serviceInstanceNew);
            }
            if (virtualMachineInstNew != null && !virtualMachineInstNew.equals(virtualMachineInstOld)) {
                Component oldComponentOfVirtualMachineInst = virtualMachineInstNew.getComponent();
                if (oldComponentOfVirtualMachineInst != null) {
                    oldComponentOfVirtualMachineInst.setVirtualMachineInst(null);
                    oldComponentOfVirtualMachineInst = em.merge(oldComponentOfVirtualMachineInst);
                }
                virtualMachineInstNew.setComponent(component);
                virtualMachineInstNew = em.merge(virtualMachineInstNew);
            }
            if (diskArrayNew != null && !diskArrayNew.equals(diskArrayOld)) {
                Component oldComponentOfDiskArray = diskArrayNew.getComponent();
                if (oldComponentOfDiskArray != null) {
                    oldComponentOfDiskArray.setDiskArray(null);
                    oldComponentOfDiskArray = em.merge(oldComponentOfDiskArray);
                }
                diskArrayNew.setComponent(component);
                diskArrayNew = em.merge(diskArrayNew);
            }
            if (activityNew != null && !activityNew.equals(activityOld)) {
                Component oldComponentOfActivity = activityNew.getComponent();
                if (oldComponentOfActivity != null) {
                    oldComponentOfActivity.setActivity(null);
                    oldComponentOfActivity = em.merge(oldComponentOfActivity);
                }
                activityNew.setComponent(component);
                activityNew = em.merge(activityNew);
            }
            if (cpuNew != null && !cpuNew.equals(cpuOld)) {
                Component oldComponentOfCpu = cpuNew.getComponent();
                if (oldComponentOfCpu != null) {
                    oldComponentOfCpu.setCpu(null);
                    oldComponentOfCpu = em.merge(oldComponentOfCpu);
                }
                cpuNew.setComponent(component);
                cpuNew = em.merge(cpuNew);
            }
            if (memoryNew != null && !memoryNew.equals(memoryOld)) {
                Component oldComponentOfMemory = memoryNew.getComponent();
                if (oldComponentOfMemory != null) {
                    oldComponentOfMemory.setMemory(null);
                    oldComponentOfMemory = em.merge(oldComponentOfMemory);
                }
                memoryNew.setComponent(component);
                memoryNew = em.merge(memoryNew);
            }
            if (isleNew != null && !isleNew.equals(isleOld)) {
                Component oldComponentOfIsle = isleNew.getComponent();
                if (oldComponentOfIsle != null) {
                    oldComponentOfIsle.setIsle(null);
                    oldComponentOfIsle = em.merge(oldComponentOfIsle);
                }
                isleNew.setComponent(component);
                isleNew = em.merge(isleNew);
            }
            if (serviceNew != null && !serviceNew.equals(serviceOld)) {
                Component oldComponentOfService = serviceNew.getComponent();
                if (oldComponentOfService != null) {
                    oldComponentOfService.setService(null);
                    oldComponentOfService = em.merge(oldComponentOfService);
                }
                serviceNew.setComponent(component);
                serviceNew = em.merge(serviceNew);
            }
            if (cpuCoreNew != null && !cpuCoreNew.equals(cpuCoreOld)) {
                Component oldComponentOfCpuCore = cpuCoreNew.getComponent();
                if (oldComponentOfCpuCore != null) {
                    oldComponentOfCpuCore.setCpuCore(null);
                    oldComponentOfCpuCore = em.merge(oldComponentOfCpuCore);
                }
                cpuCoreNew.setComponent(component);
                cpuCoreNew = em.merge(cpuCoreNew);
            }
            if (serverNew != null && !serverNew.equals(serverOld)) {
                Component oldComponentOfServer = serverNew.getComponent();
                if (oldComponentOfServer != null) {
                    oldComponentOfServer.setServer(null);
                    oldComponentOfServer = em.merge(oldComponentOfServer);
                }
                serverNew.setComponent(component);
                serverNew = em.merge(serverNew);
            }
            if (storageNew != null && !storageNew.equals(storageOld)) {
                Component oldComponentOfStorage = storageNew.getComponent();
                if (oldComponentOfStorage != null) {
                    oldComponentOfStorage.setStorage(null);
                    oldComponentOfStorage = em.merge(oldComponentOfStorage);
                }
                storageNew.setComponent(component);
                storageNew = em.merge(storageNew);
            }
            if (roomNew != null && !roomNew.equals(roomOld)) {
                Component oldComponentOfRoom = roomNew.getComponent();
                if (oldComponentOfRoom != null) {
                    oldComponentOfRoom.setRoom(null);
                    oldComponentOfRoom = em.merge(oldComponentOfRoom);
                }
                roomNew.setComponent(component);
                roomNew = em.merge(roomNew);
            }
            for (DataFile dataFileCollectionOldDataFile : dataFileCollectionOld) {
                if (!dataFileCollectionNew.contains(dataFileCollectionOldDataFile)) {
                    dataFileCollectionOldDataFile.getComponentCollection().remove(component);
                    dataFileCollectionOldDataFile = em.merge(dataFileCollectionOldDataFile);
                }
            }
            for (DataFile dataFileCollectionNewDataFile : dataFileCollectionNew) {
                if (!dataFileCollectionOld.contains(dataFileCollectionNewDataFile)) {
                    dataFileCollectionNewDataFile.getComponentCollection().add(component);
                    dataFileCollectionNewDataFile = em.merge(dataFileCollectionNewDataFile);
                }
            }
            for (SystemActionOnComponent systemActionOnComponentCollectionNewSystemActionOnComponent : systemActionOnComponentCollectionNew) {
                if (!systemActionOnComponentCollectionOld.contains(systemActionOnComponentCollectionNewSystemActionOnComponent)) {
                    Component oldComponentOfSystemActionOnComponentCollectionNewSystemActionOnComponent = systemActionOnComponentCollectionNewSystemActionOnComponent.getComponent();
                    systemActionOnComponentCollectionNewSystemActionOnComponent.setComponent(component);
                    systemActionOnComponentCollectionNewSystemActionOnComponent = em.merge(systemActionOnComponentCollectionNewSystemActionOnComponent);
                    if (oldComponentOfSystemActionOnComponentCollectionNewSystemActionOnComponent != null && !oldComponentOfSystemActionOnComponentCollectionNewSystemActionOnComponent.equals(component)) {
                        oldComponentOfSystemActionOnComponentCollectionNewSystemActionOnComponent.getSystemActionOnComponentCollection().remove(systemActionOnComponentCollectionNewSystemActionOnComponent);
                        oldComponentOfSystemActionOnComponentCollectionNewSystemActionOnComponent = em.merge(oldComponentOfSystemActionOnComponentCollectionNewSystemActionOnComponent);
                    }
                }
            }
            for (Annotation annotationCollectionOldAnnotation : annotationCollectionOld) {
                if (!annotationCollectionNew.contains(annotationCollectionOldAnnotation)) {
                    annotationCollectionOldAnnotation.setIdComponent(null);
                    annotationCollectionOldAnnotation = em.merge(annotationCollectionOldAnnotation);
                }
            }
            for (Annotation annotationCollectionNewAnnotation : annotationCollectionNew) {
                if (!annotationCollectionOld.contains(annotationCollectionNewAnnotation)) {
                    Component oldIdComponentOfAnnotationCollectionNewAnnotation = annotationCollectionNewAnnotation.getIdComponent();
                    annotationCollectionNewAnnotation.setIdComponent(component);
                    annotationCollectionNewAnnotation = em.merge(annotationCollectionNewAnnotation);
                    if (oldIdComponentOfAnnotationCollectionNewAnnotation != null && !oldIdComponentOfAnnotationCollectionNewAnnotation.equals(component)) {
                        oldIdComponentOfAnnotationCollectionNewAnnotation.getAnnotationCollection().remove(annotationCollectionNewAnnotation);
                        oldIdComponentOfAnnotationCollectionNewAnnotation = em.merge(oldIdComponentOfAnnotationCollectionNewAnnotation);
                    }
                }
            }
            for (Thresholds thresholdsCollectionNewThresholds : thresholdsCollectionNew) {
                if (!thresholdsCollectionOld.contains(thresholdsCollectionNewThresholds)) {
                    Component oldComponentOfThresholdsCollectionNewThresholds = thresholdsCollectionNewThresholds.getComponent();
                    thresholdsCollectionNewThresholds.setComponent(component);
                    thresholdsCollectionNewThresholds = em.merge(thresholdsCollectionNewThresholds);
                    if (oldComponentOfThresholdsCollectionNewThresholds != null && !oldComponentOfThresholdsCollectionNewThresholds.equals(component)) {
                        oldComponentOfThresholdsCollectionNewThresholds.getThresholdsCollection().remove(thresholdsCollectionNewThresholds);
                        oldComponentOfThresholdsCollectionNewThresholds = em.merge(oldComponentOfThresholdsCollectionNewThresholds);
                    }
                }
            }
            for (ObservedComponent observedComponentCollectionNewObservedComponent : observedComponentCollectionNew) {
                if (!observedComponentCollectionOld.contains(observedComponentCollectionNewObservedComponent)) {
                    Component oldComponentOfObservedComponentCollectionNewObservedComponent = observedComponentCollectionNewObservedComponent.getComponent();
                    observedComponentCollectionNewObservedComponent.setComponent(component);
                    observedComponentCollectionNewObservedComponent = em.merge(observedComponentCollectionNewObservedComponent);
                    if (oldComponentOfObservedComponentCollectionNewObservedComponent != null && !oldComponentOfObservedComponentCollectionNewObservedComponent.equals(component)) {
                        oldComponentOfObservedComponentCollectionNewObservedComponent.getObservedComponentCollection().remove(observedComponentCollectionNewObservedComponent);
                        oldComponentOfObservedComponentCollectionNewObservedComponent = em.merge(oldComponentOfObservedComponentCollectionNewObservedComponent);
                    }
                }
            }
            for (GpiKpiInstance gpiKpiInstanceCollectionOldGpiKpiInstance : gpiKpiInstanceCollectionOld) {
                if (!gpiKpiInstanceCollectionNew.contains(gpiKpiInstanceCollectionOldGpiKpiInstance)) {
                    gpiKpiInstanceCollectionOldGpiKpiInstance.setIdComponent(null);
                    gpiKpiInstanceCollectionOldGpiKpiInstance = em.merge(gpiKpiInstanceCollectionOldGpiKpiInstance);
                }
            }
            for (GpiKpiInstance gpiKpiInstanceCollectionNewGpiKpiInstance : gpiKpiInstanceCollectionNew) {
                if (!gpiKpiInstanceCollectionOld.contains(gpiKpiInstanceCollectionNewGpiKpiInstance)) {
                    Component oldIdComponentOfGpiKpiInstanceCollectionNewGpiKpiInstance = gpiKpiInstanceCollectionNewGpiKpiInstance.getIdComponent();
                    gpiKpiInstanceCollectionNewGpiKpiInstance.setIdComponent(component);
                    gpiKpiInstanceCollectionNewGpiKpiInstance = em.merge(gpiKpiInstanceCollectionNewGpiKpiInstance);
                    if (oldIdComponentOfGpiKpiInstanceCollectionNewGpiKpiInstance != null && !oldIdComponentOfGpiKpiInstanceCollectionNewGpiKpiInstance.equals(component)) {
                        oldIdComponentOfGpiKpiInstanceCollectionNewGpiKpiInstance.getGpiKpiInstanceCollection().remove(gpiKpiInstanceCollectionNewGpiKpiInstance);
                        oldIdComponentOfGpiKpiInstanceCollectionNewGpiKpiInstance = em.merge(oldIdComponentOfGpiKpiInstanceCollectionNewGpiKpiInstance);
                    }
                }
            }
            for (StoredValues storedValuesCollectionNewStoredValues : storedValuesCollectionNew) {
                if (!storedValuesCollectionOld.contains(storedValuesCollectionNewStoredValues)) {
                    Component oldIdComponentOfStoredValuesCollectionNewStoredValues = storedValuesCollectionNewStoredValues.getIdComponent();
                    storedValuesCollectionNewStoredValues.setIdComponent(component);
                    storedValuesCollectionNewStoredValues = em.merge(storedValuesCollectionNewStoredValues);
                    if (oldIdComponentOfStoredValuesCollectionNewStoredValues != null && !oldIdComponentOfStoredValuesCollectionNewStoredValues.equals(component)) {
                        oldIdComponentOfStoredValuesCollectionNewStoredValues.getStoredValuesCollection().remove(storedValuesCollectionNewStoredValues);
                        oldIdComponentOfStoredValuesCollectionNewStoredValues = em.merge(oldIdComponentOfStoredValuesCollectionNewStoredValues);
                    }
                }
            }
            for (VirtualMachineResources virtualMachineResourcesCollectionNewVirtualMachineResources : virtualMachineResourcesCollectionNew) {
                if (!virtualMachineResourcesCollectionOld.contains(virtualMachineResourcesCollectionNewVirtualMachineResources)) {
                    Component oldComponentOfVirtualMachineResourcesCollectionNewVirtualMachineResources = virtualMachineResourcesCollectionNewVirtualMachineResources.getComponent();
                    virtualMachineResourcesCollectionNewVirtualMachineResources.setComponent(component);
                    virtualMachineResourcesCollectionNewVirtualMachineResources = em.merge(virtualMachineResourcesCollectionNewVirtualMachineResources);
                    if (oldComponentOfVirtualMachineResourcesCollectionNewVirtualMachineResources != null && !oldComponentOfVirtualMachineResourcesCollectionNewVirtualMachineResources.equals(component)) {
                        oldComponentOfVirtualMachineResourcesCollectionNewVirtualMachineResources.getVirtualMachineResourcesCollection().remove(virtualMachineResourcesCollectionNewVirtualMachineResources);
                        oldComponentOfVirtualMachineResourcesCollectionNewVirtualMachineResources = em.merge(oldComponentOfVirtualMachineResourcesCollectionNewVirtualMachineResources);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = component.getId();
                if (findComponent(id) == null) {
                    throw new NonexistentEntityException("The component with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Component component;
            try {
                component = em.getReference(Component.class, id);
                component.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The component with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            CoolingSystem coolingSystemOrphanCheck = component.getCoolingSystem();
            if (coolingSystemOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the CoolingSystem " + coolingSystemOrphanCheck + " in its coolingSystem field has a non-nullable component field.");
            }
            Disk diskOrphanCheck = component.getDisk();
            if (diskOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the Disk " + diskOrphanCheck + " in its disk field has a non-nullable component field.");
            }
            NetworkInterface networkInterfaceOrphanCheck = component.getNetworkInterface();
            if (networkInterfaceOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the NetworkInterface " + networkInterfaceOrphanCheck + " in its networkInterface field has a non-nullable component field.");
            }
            Rack rackOrphanCheck = component.getRack();
            if (rackOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the Rack " + rackOrphanCheck + " in its rack field has a non-nullable component field.");
            }
            BusinessProcess businessProcessOrphanCheck = component.getBusinessProcess();
            if (businessProcessOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the BusinessProcess " + businessProcessOrphanCheck + " in its businessProcess field has a non-nullable component field.");
            }
            ServiceInstance serviceInstanceOrphanCheck = component.getServiceInstance();
            if (serviceInstanceOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the ServiceInstance " + serviceInstanceOrphanCheck + " in its serviceInstance field has a non-nullable component field.");
            }
            VirtualMachineInst virtualMachineInstOrphanCheck = component.getVirtualMachineInst();
            if (virtualMachineInstOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the VirtualMachineInst " + virtualMachineInstOrphanCheck + " in its virtualMachineInst field has a non-nullable component field.");
            }
            DiskArray diskArrayOrphanCheck = component.getDiskArray();
            if (diskArrayOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the DiskArray " + diskArrayOrphanCheck + " in its diskArray field has a non-nullable component field.");
            }
            Activity activityOrphanCheck = component.getActivity();
            if (activityOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the Activity " + activityOrphanCheck + " in its activity field has a non-nullable component field.");
            }
            Cpu cpuOrphanCheck = component.getCpu();
            if (cpuOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the Cpu " + cpuOrphanCheck + " in its cpu field has a non-nullable component field.");
            }
            Memory memoryOrphanCheck = component.getMemory();
            if (memoryOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the Memory " + memoryOrphanCheck + " in its memory field has a non-nullable component field.");
            }
            Isle isleOrphanCheck = component.getIsle();
            if (isleOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the Isle " + isleOrphanCheck + " in its isle field has a non-nullable component field.");
            }
            Service serviceOrphanCheck = component.getService();
            if (serviceOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the Service " + serviceOrphanCheck + " in its service field has a non-nullable component field.");
            }
            CpuCore cpuCoreOrphanCheck = component.getCpuCore();
            if (cpuCoreOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the CpuCore " + cpuCoreOrphanCheck + " in its cpuCore field has a non-nullable component field.");
            }
            Server serverOrphanCheck = component.getServer();
            if (serverOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the Server " + serverOrphanCheck + " in its server field has a non-nullable component field.");
            }
            Storage storageOrphanCheck = component.getStorage();
            if (storageOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the Storage " + storageOrphanCheck + " in its storage field has a non-nullable component field.");
            }
            Room roomOrphanCheck = component.getRoom();
            if (roomOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the Room " + roomOrphanCheck + " in its room field has a non-nullable component field.");
            }
            Collection<SystemActionOnComponent> systemActionOnComponentCollectionOrphanCheck = component.getSystemActionOnComponentCollection();
            for (SystemActionOnComponent systemActionOnComponentCollectionOrphanCheckSystemActionOnComponent : systemActionOnComponentCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the SystemActionOnComponent " + systemActionOnComponentCollectionOrphanCheckSystemActionOnComponent + " in its systemActionOnComponentCollection field has a non-nullable component field.");
            }
            Collection<Thresholds> thresholdsCollectionOrphanCheck = component.getThresholdsCollection();
            for (Thresholds thresholdsCollectionOrphanCheckThresholds : thresholdsCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the Thresholds " + thresholdsCollectionOrphanCheckThresholds + " in its thresholdsCollection field has a non-nullable component field.");
            }
            Collection<ObservedComponent> observedComponentCollectionOrphanCheck = component.getObservedComponentCollection();
            for (ObservedComponent observedComponentCollectionOrphanCheckObservedComponent : observedComponentCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the ObservedComponent " + observedComponentCollectionOrphanCheckObservedComponent + " in its observedComponentCollection field has a non-nullable component field.");
            }
            Collection<StoredValues> storedValuesCollectionOrphanCheck = component.getStoredValuesCollection();
            for (StoredValues storedValuesCollectionOrphanCheckStoredValues : storedValuesCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the StoredValues " + storedValuesCollectionOrphanCheckStoredValues + " in its storedValuesCollection field has a non-nullable idComponent field.");
            }
            Collection<VirtualMachineResources> virtualMachineResourcesCollectionOrphanCheck = component.getVirtualMachineResourcesCollection();
            for (VirtualMachineResources virtualMachineResourcesCollectionOrphanCheckVirtualMachineResources : virtualMachineResourcesCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Component (" + component + ") cannot be destroyed since the VirtualMachineResources " + virtualMachineResourcesCollectionOrphanCheckVirtualMachineResources + " in its virtualMachineResourcesCollection field has a non-nullable component field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<DataFile> dataFileCollection = component.getDataFileCollection();
            for (DataFile dataFileCollectionDataFile : dataFileCollection) {
                dataFileCollectionDataFile.getComponentCollection().remove(component);
                dataFileCollectionDataFile = em.merge(dataFileCollectionDataFile);
            }
            Collection<Annotation> annotationCollection = component.getAnnotationCollection();
            for (Annotation annotationCollectionAnnotation : annotationCollection) {
                annotationCollectionAnnotation.setIdComponent(null);
                annotationCollectionAnnotation = em.merge(annotationCollectionAnnotation);
            }
            Collection<GpiKpiInstance> gpiKpiInstanceCollection = component.getGpiKpiInstanceCollection();
            for (GpiKpiInstance gpiKpiInstanceCollectionGpiKpiInstance : gpiKpiInstanceCollection) {
                gpiKpiInstanceCollectionGpiKpiInstance.setIdComponent(null);
                gpiKpiInstanceCollectionGpiKpiInstance = em.merge(gpiKpiInstanceCollectionGpiKpiInstance);
            }
            em.remove(component);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Component> findComponentEntities() {
        return findComponentEntities(true, -1, -1);
    }

    public List<Component> findComponentEntities(int maxResults, int firstResult) {
        return findComponentEntities(false, maxResults, firstResult);
    }

    private List<Component> findComponentEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Component.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Component findComponent(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Component.class, id);
        } finally {
            em.close();
        }
    }

    public int getComponentCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Component> rt = cq.from(Component.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
